<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_category_dbc.class.php');


class VPage_Ordering_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('category');
		$this->add_transit_var('vpage');
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('top', Tangra_Parameter_Method::GET, 'top'), 'top_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('bottom', Tangra_Parameter_Method::GET, 'bottom'), 'bottom_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('reset', Tangra_Parameter_Method::GET, 'reset'), 'reset_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('recalc', Tangra_Parameter_Method::GET, 'recalc'), 'recalc_event');

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('up', Tangra_Parameter_Method::GET, 'up'), 'up_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('down', Tangra_Parameter_Method::GET, 'down'), 'down_event');


		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('manual', Tangra_Parameter_Method::GET, 'manual'), 'manual_event');
	}


	protected function top_event($top) {
		$dbc = $this->get_dbc();

		$vpage_id = $this->get_transit_var('vpage');
		$category_id = $this->get_transit_var('category');

		try {
			$dbc->start_trans();
			TCMS_VPage_Category_DBC::move_top($dbc, $vpage_id, $category_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');

		return $view;
	}


	protected function bottom_event($bottom) {
		$dbc = $this->get_dbc();

		$vpage_id = $this->get_transit_var('vpage');
		$category_id = $this->get_transit_var('category');

		try {
			$dbc->start_trans();
			TCMS_VPage_Category_DBC::move_bottom($dbc, $vpage_id, $category_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');

		return $view;
	}


	protected function reset_event($reset) {
		$dbc = $this->get_dbc();
		$category_id = $this->get_transit_var('category');
		try {
			$dbc->start_trans();
			TCMS_VPage_Category_DBC::reset_ordering($dbc, $category_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');

		return $view;
	}


	protected function recalc_event($recalc) {
		$dbc = $this->get_dbc();
		$category_id = $this->get_transit_var('category');
		try {
			$dbc->start_trans();
			TCMS_VPage_Category_DBC::recalc_ordering($dbc, $category_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');

		return $view;
	}


	protected function up_event($up) {
		$dbc = $this->get_dbc();

		$vpage_id = $this->get_transit_var('vpage');
		$category_id = $this->get_transit_var('category');

		try {
			$dbc->start_trans();
			TCMS_VPage_Category_DBC::move_up($dbc, $vpage_id, $category_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}


		$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');
		return $view;
	}


	protected function down_event($down) {
		$dbc = $this->get_dbc();

		$vpage_id = $this->get_transit_var('vpage');
		$category_id = $this->get_transit_var('category');

		try {
			$dbc->start_trans();
			TCMS_VPage_Category_DBC::move_down($dbc, $vpage_id, $category_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}


		$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');
		return $view;
	}


	protected function manual_event($manual) {
		$dbc = $this->get_dbc();

		if ($manual) {
			$vpage_id = $this->get_transit_var('vpage');
			$category_id = $this->get_transit_var('category');

			$vpage_cat = new TCMS_VPage_Category_DBC();
			if ($vpage_cat->load_by_vpage_and_category($dbc, $vpage_id, $category_id)) {
				$vpage_cat->set_ordering($manual);
				try {
					$dbc->start_trans();
					$vpage_cat->save($dbc);
					$dbc->complete_trans();
				} catch (Exception $e) {
					$dbc->fail_trans();
					$dbc->complete_trans();
					throw $e;
				}

				$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');
			} else {
				$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');
			}
		} else {
			$view = $this->default_event();
		}


		return $view;
	}


	protected function default_event() {
		$dbc = $this->get_dbc();

		$vpage_id = $this->get_transit_var('vpage');
		$category_id = $this->get_transit_var('category');

		$category = new TCMS_Category_DBC();

		if ($category->load_by_id($dbc, $category_id)) {
			$this->export('category', $category);

			$vpage = new TCMS_VPage_DBC();
			if ($vpage->load_by_id($dbc, $vpage_id)) {
				$this->export('vpage', $vpage);

				$vpage_cat = new TCMS_VPage_Category_DBC();
				if ($vpage_cat->load_by_vpage_and_category($dbc, $vpage_id, $category_id)) {
					$this->export('ordering', $vpage_cat->get_ordering());
					$view = $this->get_view('default');
				} else {
					$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');
				}
			} else {
				$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');
			}
		} else {
			$view = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php');
		}

		return $view;
	}
}